package com.gec.system.utils;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.gec.system.config.VodConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.OutputStream;
@Data
@Component
public class VodTemplate {
    @Autowired
    private VodConfig config;
    public DefaultAcsClient defaultAcsClient(){
        DefaultProfile profile = DefaultProfile.getProfile(config.getRegionId(),
                config.getAccessKeyId(),
                config.getAccessKeySecret());
        return new DefaultAcsClient(profile);
    }
    public String uploadVideo(String fileName, InputStream inputStream){
        String title = fileName.substring(0, fileName.indexOf("."));
        UploadStreamRequest uploadStreamRequest = new UploadStreamRequest(
                config.getAccessKeyId(),
                config.getAccessKeySecret(),
                title,
                fileName,
                inputStream);
        uploadStreamRequest.setStorageLocation("outin-86fa3ef87bec11efbdaa00163e040f9b.oss-cn-shenzhen.aliyuncs.com");
        uploadStreamRequest.setApiRegionId("cn-shenzhen");
        UploadVideoImpl uploadVideo = new UploadVideoImpl();
        UploadStreamResponse response = uploadVideo.uploadStream(uploadStreamRequest);
        String id = response.getVideoId();
        return id;

    }
    public GetVideoPlayAuthResponse getVideoPlayAuthResponse(String videoId) throws ClientException {
        GetVideoPlayAuthRequest getVideoPlayAuthRequest = new GetVideoPlayAuthRequest();
        getVideoPlayAuthRequest.setVideoId(videoId);
        return defaultAcsClient().getAcsResponse(getVideoPlayAuthRequest);

    }
}
